package org.csu.jpetstoremanagement.controller;

import com.alibaba.fastjson.JSON;
import org.csu.jpetstoremanagement.domain.Account;
import org.csu.jpetstoremanagement.domain.Category;
import org.csu.jpetstoremanagement.domain.Log;
import org.csu.jpetstoremanagement.service.AccountService;
import org.csu.jpetstoremanagement.service.CatalogService;
import org.csu.jpetstoremanagement.service.LogService;
import org.csu.jpetstoremanagement.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@SessionAttributes({"categories", "products"})
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private CatalogService catalogService;

    @Autowired
    private LogService logService;

    @Autowired
    private OrderService orderService;

    @PostMapping("/login")
    public String login(String username, String password, HttpSession session, Model model,Map<String, Object> map) {
        Account account = accountService.getAccountByUsernameAndPassword(username, DigestUtils.md5DigestAsHex(password.getBytes()));

            session.setAttribute("account", account);
            List<Category> categories = catalogService.getCategoryList();
            model.addAttribute("categories", categories);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            String day = formatter.format(date) + "%";

            int accountNumber = accountService.getAccountNumber();
            int viewNumber = logService.getViewNumber();
            int orderNumber = orderService.getOrderNumber();
            int unSent = orderService.getUnSentNumber();
            int todayViewNumber = logService.getTodayViewNumber(day);
            int todayOrderNumber = orderService.getTodayOrderNumber(day);
            int alreadySent = orderService.getAlreadyNumber();

            map.put("accountNumber",accountNumber);
            map.put("viewNumber",viewNumber);
            map.put("orderNumber",orderNumber);
            map.put("unSent",unSent);
            map.put("todayViewNumber",todayViewNumber);
            map.put("todayOrderNumber",todayOrderNumber);
            map.put("alreadySent",alreadySent);

            return "/common/home";

    }

    @GetMapping("/exit")
    public String exit(HttpSession session) {
        session.removeAttribute("account");
        return "/account/sign_in";
    }

    @GetMapping("/accounts")
    public String getAllAccount(Model model) {
        List<Account> accounts = accountService.getAllAccount();
        model.addAttribute("accounts", accounts);
        List<Category> categories = catalogService.getCategoryList();
        model.addAttribute("categories", categories);
        return "/account/accounts";
    }

    @GetMapping("/accounts/{id}")
    @ResponseBody
    public String getAccount(@PathVariable("id") String accountId) {
        System.out.println(accountId);
        Account account = accountService.getAccountByUsername(accountId);
        return JSON.toJSONString(account);
    }

    @PutMapping("/accounts/{id}")
    public String updateAccount(@PathVariable("id") String accountId, Account account, Model model) {
        if (account.getUsername().equals(accountId)) {
            accountService.updateAccount(account);
            return "redirect:/accounts";
        } else {
            model.addAttribute("msg", "Request does not match actual data");
            return "/common/error";
        }
    }

    @PutMapping("/accounts/{id}/password")
    public String updateAccountPassword(@PathVariable("id") String accountId, String password, String repeatPassword, Model model) {
        if (password.equals(repeatPassword)) {
            Account account = accountService.getAccountByUsername(accountId);
            account.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
            accountService.updateAccount(account);
            return "redirect:/accounts";
        } else {
            model.addAttribute("msg", "两次密码输入不一致");
            return "/common/error";
        }
    }

    @GetMapping("/log")
    public String getAllLog(Model model) {
        List<Log> logs = logService.getAllLog();
        model.addAttribute("logs", logs);
        List<Category> categories = catalogService.getCategoryList();
        model.addAttribute("categories", categories);
        return "/account/log";
    }

    @GetMapping("/log/{date}")
    @ResponseBody
    public String getLog(@PathVariable("date") String date ) {
        System.out.println(date);
        Log log = logService.getLogByDate(date);
        return JSON.toJSONString(log);
    }

    @GetMapping("/deleteLog")
    public String deleteCategory(String date, Model model) {
        System.out.println(date);
        logService.deleteLog(date);
        List<Log> logs = logService.getAllLog();
        model.addAttribute("logs", logs);
        return "redirect:/log";
    }

    @GetMapping("/home")
    public String home(Map<String, Object> map) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String day = formatter.format(date) + "%";

        int accountNumber = accountService.getAccountNumber();
        int viewNumber = logService.getViewNumber();
        int orderNumber = orderService.getOrderNumber();
        int unSent = orderService.getUnSentNumber();
        int todayViewNumber = logService.getTodayViewNumber(day);
        int todayOrderNumber = orderService.getTodayOrderNumber(day);
        int alreadySent = orderService.getAlreadyNumber();

        map.put("accountNumber",accountNumber);
        map.put("viewNumber",viewNumber);
        map.put("orderNumber",orderNumber);
        map.put("unSent",unSent);
        map.put("todayViewNumber",todayViewNumber);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("alreadySent",alreadySent);
        return "/common/home";
    }

}
